package wt.hpad.model.remote;

import android.text.TextUtils;

import java.lang.ref.WeakReference;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;
import wt.hpad.ConstantFields;
import wt.hpad.MainApp;
import wt.hpad.model.bean.Dish;
import wt.hpad.model.bean.DishDetail;
import wt.hpad.model.bean.DishType;
import wt.hpad.model.bean.ResponseMessage;
import wt.hpad.model.bean.TableInfo;
import wt.hpad.utils.DialogUtil;

import static wt.hpad.ConstantFields.SERVER;

/**
 * retrofit 的 http 请求
 * Created by wt on 11.20.
 */

public class HttpRequest {
    private WeakReference<HttpUtils> mHttpUtils = null;

    private HttpService getHttpService() {
        if (!TextUtils.isEmpty(SERVER)) {
            mHttpUtils = new WeakReference<>(new HttpUtils(ConstantFields.HTTP_URL));
            return (HttpService) mHttpUtils.get().setupRetrofit(HttpService.class);
        } else {
            DialogUtil.showInToast(MainApp.getContext(), "服务器地址未设置");
            return null;
        }
    }

    /**
     * 登录并验证是否合法设备
     */
    public void handleLogin(Observer<? super Integer> observer) {
        HttpService httpService = getHttpService();
        if (httpService != null) {
            Call<ResponseMessage> call = httpService.login(ConstantFields.DEVICEID); // 测试用 deviceID
            mHttpUtils.get().loginAndCheckDeviceID(call)
                    .observeOn(Schedulers.newThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(observer);
        }
    }

    /**
     * 菜品分类
     */
    public void handleType(Observer<List<DishType>> observer) {
        HttpService httpService = getHttpService();
        if (httpService != null) {
            Call<ResponseMessage> call = httpService.getDishType();
            mHttpUtils.get().getDishType(call)
                    .observeOn(Schedulers.newThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(observer);
        }
    }

    /**
     * 点菜
     */
    public void handleOrder(Observer<? super Integer> observer, String tableID, String waiter, String pNum, String content) {
        HttpService httpService = getHttpService();
        if (httpService != null) {
            Call<ResponseMessage> call = httpService.placeOrder(tableID, waiter, pNum, content);
            mHttpUtils.get().placeOrder(call)
                    .observeOn(Schedulers.newThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(observer);
        }
    }

    /**
     * 桌台
     */
    public void handleTableInfo(Observer<List<TableInfo>> observer) {
        HttpService httpService = getHttpService();
        if (httpService != null) {
            Call<ResponseMessage> call = httpService.getAllTableInfo();
            mHttpUtils.get().getAllTableInfo(call)
                    .observeOn(Schedulers.newThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(observer);
        }
    }

    /**
     * 菜单
     */
    public void handleMenu(Observer<List<Dish>> observer) {
        HttpService httpService = getHttpService();
        if (httpService != null) {
            Call<ResponseMessage> call = httpService.getAllDishes();
            mHttpUtils.get().getAllDishes(call)
                    .observeOn(Schedulers.newThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(observer);
        }
    }

    /**
     * 进入桌台
     */
    public void handleEnterTable(Observer<List<DishDetail>> observer, String tableId) {
        HttpService httpService = getHttpService();
        if (httpService != null) {
            Call<ResponseMessage> call = httpService.getTableDishes(tableId);
            mHttpUtils.get().getTableDishes(call)
                    .observeOn(Schedulers.newThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(observer);
        }
    }

    /**
     * 退菜
     */
    public void handleRetreatDish(Observer<Integer> observer, int sid, int num, String tipOfException) {
        HttpService httpService = getHttpService();
        if (httpService != null) {
            Call<ResponseMessage> call = httpService.dishRetreat(sid, num, tipOfException);
            mHttpUtils.get().retreatDish(call)
                    .observeOn(Schedulers.newThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(observer);
        }
    }

    public void stopHandlerAction() {
        if (mHttpUtils != null) {
            mHttpUtils.get().stopHttpRequest();
        }
    }
}
